package app.view.mediators
{
	import app.AppConstants;
	import app.model.flare.vos.ActionVO;
	import app.model.flare.vos.StateVO;
	import app.view.ICommitable;
	import app.view.forms.ActionForm;
	import app.view.forms.StateForm;
	
	import flash.events.Event;
	import flash.events.KeyboardEvent;
	import flash.events.MouseEvent;
	import flash.ui.Keyboard;
	
	import mx.controls.Button;
	import mx.core.Application;
	import mx.core.Container;
	import mx.events.CloseEvent;
	import mx.events.FlexEvent;
	
	import org.puremvc.as3.multicore.interfaces.INotification;
	import org.puremvc.as3.multicore.patterns.mediator.Mediator;

	public class StateFormMediator extends Mediator
	{
		
		public static const NAME:String ="StateFormMediator";
		
		private var application:Application;
		private var isCreated:Boolean;
		private var state:StateVO;
		private var panelMap:Array;

		public function StateFormMediator( view:Container )
		{
			super(NAME, view);
		}
		
		 private  function addListeners():void{
			view.addEventListener(MouseEvent.CLICK, onClick);
			view.addEventListener(KeyboardEvent.KEY_UP, onKeyUp);
		}
		
		 private function removeListeners():void{
			view.removeEventListener(MouseEvent.CLICK, onClick);
			view.removeEventListener(KeyboardEvent.KEY_UP, onKeyUp);
		}
		
		override public function listNotificationInterests():Array{
			return [ AppConstants.POPUP_STATE_FORM ]
		}
		
		override public function handleNotification(notification:INotification):void{
			switch(notification.getName()){
				case AppConstants.POPUP_STATE_FORM:
					state = StateVO( notification.getBody() );
					show( );
					break;
			}
		}
		
		
		
		public function show(  ):void{
			addListeners();
			addForms();
		}
		
		private function addForms():void{
			view.addChild( createStateForm( state ) );
			for each (var action:ActionVO in state.targets){
				view.addChild( createActionForm( action ) );
			}
		}
		
		private function createStateForm( state:StateVO ):StateForm{
			var form:StateForm = new StateForm();
			form.data = state;
			form.addEventListener(CloseEvent.CLOSE, onClose);
			form.addEventListener(FlexEvent.CREATION_COMPLETE, onStateFormCreationComplete);
			return form;
		}
		
		private function createActionForm( action:ActionVO=null ):ActionForm{
			var form:ActionForm = new ActionForm();
			if(action)form.data = action;
			return form;
		}
		
		private function position():void{
			view.x = 10;
			view.y = 10;
		}
		
		private function onStateFormCreationComplete(event:FlexEvent):void{
			var target:StateForm = StateForm( event.target );
			target.input.setFocus();
			target.removeEventListener(FlexEvent.CREATION_COMPLETE, onStateFormCreationComplete);
			target.initial.addEventListener(Event.CHANGE, onInitialChange);

		}
		
		private function onInitialChange(event:Event):void{
			sendNotification(AppConstants.CHANGE_INITIAL_STATE, state)
		}

		private function onClose(event:CloseEvent = null):void{
			hide( );
		}
		
		private function onKeyUp(event:KeyboardEvent):void{
			
			switch(true){
				case (event.keyCode == Keyboard.ENTER):
					submit( );
					hide();
					break;
					
				case (event.keyCode == Keyboard.ESCAPE):
					hide( );	
					break;
					
				case (event.keyCode == Keyboard.DELETE):
					deleteState();
					break;
					
				case (event.keyCode == Keyboard.F1):
					addAction();
					break; 
			}
		}
		
		private function onClick(event:MouseEvent):void{
			var target:Button = event.target as Button;
			if(!target)return;
			switch( target.name){
				case "deleteState":
					deleteState();
					break;
				case "submit":
					submit();
					hide();
					break;
				case "deleteAction":
					deleteAction( ActionVO( Container(target.parent.parent).data ) );
					break;
					
				case "addAction":
					addAction();
					break;
					
					
								
			} 
		}
		
		private function addAction():void{
			var action:ActionVO = new ActionVO()
			state.addAction(action);
			view.addChild(createActionForm(action));
			//sendUpdateNote();
			//sendSetCurrentNodeNote();
		}
		
		private function deleteAction(action:ActionVO):void{
			//sendDeleteActionNote(action);
			state.removeAction(action.name);
			sendUpdateNote();
			sendSetCurrentNodeNote();
		}
		
		private function deleteState():void{
			sendDeleteStateNote();
			sendUpdateNote();
			hide();
		}
		
		
		private function sendSetCurrentNodeNote():void{
			sendNotification(AppConstants.SET_CURRENT_NODE, state);
		}
		
		private function sendUpdateNote():void{
			sendNotification(AppConstants.UPDATE_VISUALISATION);
		}
		
		private function sendDeleteStateNote():void{
			sendNotification(AppConstants.REMOVE_STATE, null, state.name);
		}
		private function sendDeleteActionNote(action:ActionVO):void{
			sendNotification(AppConstants.REMOVE_ACTION, state.name, action.name);
		}
		
		private function submit():void{
			var update:Boolean,
				n:int,
				form:ICommitable;
			while(n<view.numChildren){
				form = ICommitable( view.getChildAt( n ) );
				update = ( form.commit() ) ? true : update;
				n++;
			}
			if(update)sendUpdateNote();
		}
		
		

		public function hide(  ):void{
			removeListeners();
			view.getChildAt(0).removeEventListener(CloseEvent.CLOSE, onClose);
			view.removeAllChildren();
			sendNotification(AppConstants.EDIT_CLOSE)
		}
		
		public function get view():Container{
			return Container( viewComponent );
		}
		
	}
}